﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace MultiTenant.FreeSql
{
    public class AuthUser : IAuthUser
    {

        private readonly IHttpContextAccessor _accessor;
        public AuthUser(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        /// <summary>
        /// 用户Id
        /// </summary>

        public long Id
        {
            get
            {
                var id = _accessor?.HttpContext?.User?.FindFirst(ClaimAttributes.UserId);

                long.TryParse(id?.Value, out long tid);
                return tid;
            }
        }


        /// <summary>
        /// 用户Data
        /// </summary>

        public List<long>? UserData
        {
            get
            {
                var userdatas = _accessor?.HttpContext?.User?.FindFirst(ClaimAttributes.UserData);

                var rs = userdatas?.Value?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                return rs?.Select(x => {
                    long.TryParse(x, out long b);
                    return b;
                }).ToList();
            }
        }


        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimAttributes.UserName);

                if (name != null && name.Value.NotNull())
                {
                    return name.Value;
                }

                return "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string? DisplayName
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimAttributes.DisplayName);

                if (name != null && name.Value.NotNull())
                {
                    return name.Value;
                }

                return null;
            }
        }
        /// <summary>
        /// 总部ID
        /// </summary>
        public long? OrgId
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimAttributes.OrgId);

                long.TryParse(name?.Value, out long pid);
                return pid;
            }
        }

        /// <summary>
        /// 部门编码
        /// </summary>
        public string? OrgCode
        {
            get
            {
                return _accessor?.HttpContext?.User?.FindFirst(ClaimAttributes.OrgCode)?.Value;
            }
        }

        /// <summary>
        /// 岗位代码
        /// </summary>
        public List<string>? RoleCodes
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimTypes.Role);
                var rs = name?.Value?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                return rs?.ToList();
            }
        }
        /// <summary>
        /// 不同登录的模块组
        /// </summary>
        public string? Group
        {
            get
            {
                var name = _accessor?.HttpContext?.User?.FindFirst(ClaimTypes.GroupSid);
                return name?.Value;
            }
        }



    }


}
